﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ClientApp
{
    public partial class CandidateRegForm : Form
    {
        public CandidateRegForm()
        {
            InitializeComponent();
        }

        private void CandidateRegistrationDoneButton_Click(object sender, EventArgs e)
        {
            if (FirstNameMaskedTextBox.Text == "" ||
                LastNameMaskedTextBox.Text == "" ||
                MiddleNameMaskedTextBox.Text == "" ||
                ActivityComboBox.SelectedIndex < 0 ||
                AddressMaskedTextBox.Text == "" ||
                SalaryMaskedTextBox.Text == "" ||
                LoginTextBox.Text == "")
            {
                MessageBox.Show("Заполните все поля формы.", "Ошибка");
                return;
            }

            if (DatabaseAccess.Instance.CandidateExists(LoginTextBox.Text))
            {
                MessageBox.Show("Введенный логин уже существует в системе", "Ошибка");
                return;
            }

            List<Skill> checked_skills = (from object item in SkillsCheckedListBox.CheckedItems 
                                          select DatabaseAccess.Instance.SkillByTitle(item as string)).ToList();

            DatabaseAccess.Instance.Add(
                new Candidate(LastNameMaskedTextBox.Text,
                              FirstNameMaskedTextBox.Text,
                              MiddleNameMaskedTextBox.Text,
                              DatabaseAccess.Instance.ActivityByTitle(ActivityComboBox.SelectedText),
                              AddressMaskedTextBox.Text,
                              checked_skills, 
                              Convert.ToUInt32(SalaryMaskedTextBox.Text),
                              LoginTextBox.Text));

            MessageBox.Show("Соискатель успешно зарегистрирован.");
            Close();
        }
    }
}
